import common from '../../utils/common.js'
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    editType:false,
    name: '',
    county_id:3284,
    tab: {
      book_lists: [],
      beginrow: 0,
      islastline: 0,
      update: false,
      loading: false
    },
    $toast: {
      show: false,
      text: ''
    }
  },
  searchChange(e) {
    let self = this
    self.setData({
      name: e.detail.value
    })
    self.getSchools(e.detail.value, 0)
  },
  getSchools(name, beginrow) {
    let self = this
    let tab = self.data.tab
    app.getToken(token => {
      let data = {
        token: token,
        org_name: name,
        beginrow: beginrow,
        pagecount: 30,
        county_id: self.data.county_id
      }
      app.getApi.getSchools(data).then(res => {
        if (res.code == 3000) {
          if (beginrow == 0) { //初始化
            tab.book_lists = res.data
            tab.update = true
          } else {
            tab.book_lists = tab.book_lists.concat(res.data)
          }
          tab.islastline = res.islastline
          tab.beginrow = beginrow
          tab.loading = false
          self.setData({
            tab: tab,
            name: name
          })
        }
      }).catch(Error => {
        console.log(Error)
      })
    })
  },
  cancel() {
    let self = this
    self.setData({
      name: ''
    })
  },
  changeEdit(){
    this.setData({
      editType:!this.data.editType
    })
  },
  onInput(e) {
    let number = e.detail.value;
    this.setData({
      name: number
    });
  },
  submit(e){
    let name
    let self = this
    if (e.currentTarget.dataset.name){
      name = e.currentTarget.dataset.name
      self.goBack(name)      
    }else{
      name = this.data.name
      app.getToken(token => {
        app.getApi.addSchool({
          token: token,
          org_name: name,
          county_id: self.data.county_id
        }).then(res => {
          if (res.code == 3000) {
            self.goBack(name)
          } else {
            common.showToast(self, res.message)
          }
        }).catch(Error => {
          common.showToast(self, Error)
        })
      })
    }
  },
  goBack(name){
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setData({
      [`userInfo.school_name`]: name
    }, () => {
      wx.navigateBack({
        delta: 1
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this 
    self.setData({
      name:'',
      county_id: options.county_id || 3284
    },()=>{
      self.getSchools('', 0)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let self = this
    let tab = self.data.tab
    if (tab.loading || tab.islastline) { //是否到底(0：否，1：是)
      return
    }
    this.setData({
      [`tab.loading`]: true
    }, () => {
      let beginrow = self.data.tab[`beginrow`]
      setTimeout(() => { self.getSchools(self.data.name, beginrow + 30) }, 500)
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '天天享读，让图书传递爱',
      imageUrl: '../../assets/images/index_s.png',
      path: '/pages/index/index'
    }
  }
})